/***************************************************************************
 *   Copyright (C) 2007 by Anders Nore									   								 *
 *   andenore@start.no   												   												 *
 *                                                                         *
 *   Permission is hereby granted, free of charge, to any person obtaining *
 *   a copy of this software and associated documentation files (the       *
 *   "Software"), to deal in the Software without restriction, including   *
 *   without limitation the rights to use, copy, modify, merge, publish,   *
 *   distribute, sublicense, and/or sell copies of the Software, and to    *
 *   permit persons to whom the Software is furnished to do so, subject to *
 *   the following conditions:                                             *
 *                                                                         *
 *   The above copyright notice and this permission notice shall be        *
 *   included in all copies or substantial portions of the Software.       *
 *                                                                         *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       *
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    *
 *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*
 *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR     *
 *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
 *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
 *   OTHER DEALINGS IN THE SOFTWARE.                                       *
 ***************************************************************************/

#include "mcastchat.h"
#include "extra.h"
#include <sys/socket.h>

int addgroup(struct sockaddr *mcastgroup)
{
	int i;
	struct sockaddr sa;
	bzero(&sa, sizeof(sa));
	
	for( i=0; i<MAX_GROUP_JOINS; i++) {
		if(memcmp(&sa, &joined_groups[i], sizeof(sa)) == 0) {
			memcpy(&joined_groups[i], mcastgroup, sizeof(*mcastgroup));
			num_joined_groups++;
			break;
		}
	}
	
	if(i == (i - 1)) {
		return -1;
	} else
		return 0;
}

int removegroup(struct sockaddr *mcastgroup)
{
	int i;
	struct sockaddr sa;
	bzero(&sa, sizeof(sa));
	
	for( i=0; i<MAX_GROUP_JOINS; i++) {
		if( memcmp(&joined_groups[i], mcastgroup, sizeof(*mcastgroup)) == 0 ) {
// 			DEBUG_PRINT_STR("Leaving group:", Sock_ntop( mcastgroup, sizeof(*mcastgroup)));
			joined_groups[i] = sa;
			num_joined_groups--;
			break;
		}
	}
	
	return 0;
}

/* determine wheter a multicastgroup is already in the joined_groups table */
int ingroup(struct sockaddr *mcastgroup)
{
	int i;
	
	for( i=0; i < MAX_GROUP_JOINS; i++) {
		if(memcmp(&joined_groups[i], mcastgroup, sizeof(*mcastgroup)) == 0)
			return 1;
	}
	
	return 0;
}

#define LOCK_HISTORY() \
	pthread_mutex_lock( &history_mutex )
#define UNLOCK_HISTORY() \
	pthread_mutex_unlock( &history_mutex )

void initializeHistory(void)
{
	pthread_mutex_init(&history_mutex, NULL);
	LOCK_HISTORY();
	bzero(&history, sizeof(&history));	
	bzero(history, sizeof(history));
	UNLOCK_HISTORY();
}

int addHistory(char *string)
{
	LOCK_HISTORY();
	
// 	DEBUG_PRINT_INT("strlen:", strlen(&history[history_index]));
// 	bzero(history[history_index], strlen(history[history_index]));
// 	DEBUG_PRINT_INT("History_index", history_index);
	strncpy((char *)&history[history_index], string, strlen(string));
	
	if(++history_index > (MAX_HISTORY - 1))
		history_index = 0;
	
	UNLOCK_HISTORY();
}

char *getHistory(int index)
{
	if(index < 0 || index > (MAX_HISTORY - 1))
		return NULL;
		
	char *ret;
	LOCK_HISTORY();
	
	if( ((history_index - 1) - index) < 0)
		index = 9 - index;
	else
		index = history_index - (index + 1);
	
	ret = history[index];	
	
	UNLOCK_HISTORY();
	return ret;
}


